package com.thinkfit.jy.activity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.DeviceTypeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

/**
 * 音量调节
 */
public class SoundActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View, View.OnTouchListener {

    private AudioManager mAudioManager;
    private RelativeLayout.LayoutParams mLayoutParams;
    private View mProgressView;
    private int mMaxHeight;
    private int mNeedChangeProgress, mMinHeight;
    private int mMaxVolume;
    private int mCurrentStyle;
    private HashMap<String, Integer> mMap;

    private int mType;


    public void onCreate(Bundle savedInstanceState) {
        mType = getIntent().getIntExtra(Constants.BUNDLE_EXTRA, 0);

        super.onCreate(savedInstanceState);
    }


    @Override
    protected int getLayoutId() {
        return mType == 0 ? R.layout.activity_sound_0 : R.layout.activity_sound_1;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.rl);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = getDisplayMetrics().widthPixels;
        params.height = getDisplayMetrics().heightPixels;
        getWindow().setAttributes(params);
        mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
//        if (mCurrentStyle == 3) {
//            Bitmap bmp = (Bitmap) DataManager.Companion.getInstance().getObject();
//            getWindow().setBackgroundDrawable(new BitmapDrawable(context.getResources(), bmp));
//        } else {
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        //      }
        findViewById(R.id.viewBack).setOnTouchListener(this);
        mProgressView = findViewById(R.id.viewProgress);
        mLayoutParams = (RelativeLayout.LayoutParams) mProgressView.getLayoutParams();
        mMaxHeight = BaseUtils.StaticParams.dp2px(this, 112);
        mMinHeight = BaseUtils.StaticParams.dp2px(this, 10);
        mNeedChangeProgress = BaseUtils.StaticParams.dp2px(this, 102);

        int currentVolume = getAudioManager().getStreamVolume(AudioManager.STREAM_MUSIC);
        //媒体音量 STREAM_MUSIC    STREAM_RING 铃声音量
        mMaxVolume = getAudioManager().getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        resetProgressView(mMaxVolume - currentVolume, mMaxVolume);
    }


    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   SoundActivity ");
        DeviceTypeUtils.showBackButton(getApplicationContext());
    }

    private AudioManager getAudioManager() {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        }
        return mAudioManager;
    }


    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl:
                finish();
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                resetProgressView(event.getY(), v.getHeight());
                resetVolume(event.getY(), v.getHeight());
                break;
        }
        return true;
    }

    private void resetProgressView(float y, int viewHeight) {
        if (y < 0) {
            y = 0;
        } else if (y > viewHeight) {
            y = viewHeight;
        }
        float progress = (viewHeight - y) / viewHeight;
        int height = (int) (progress * mMaxHeight);
        if (height > mMaxHeight) {
            height = mMaxHeight;
        } else if (height < mMinHeight) {
            height = mMinHeight;
        }
        mProgressView.setBackgroundResource(height > mNeedChangeProgress ?
                R.drawable.shape_efc638_10 : R.drawable.shape_efc638_10_bottom);
        mLayoutParams.height = height;
        mProgressView.setLayoutParams(mLayoutParams);
    }

    /**
     * 调整音量
     * @param y
     * @param viewHeight
     */
    private void resetVolume(float y, int viewHeight) {
        float progress = (viewHeight - y) / viewHeight;
        int volume = (int) (progress * mMaxVolume + 0.5f);
        if (volume > mMaxVolume) {
            volume = mMaxVolume;
        } else if (volume < 1) {
            volume = 1;
        }
        if (mCurrentStyle == 1 || mCurrentStyle == 3) {
            if (mMap == null) {
                mMap = new HashMap<>();
            }

            mMap.put(EventBusEvent.VOICE_CHANGE, (int) (progress * 100));
            EventBus.getDefault().post(mMap);
        }
        //设置铃声音量
        getAudioManager().setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);


    }
}